package com.zmm.util.password;

import com.zmm.common.constant.Constant;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Random;

/**
 * description:
 * name NumberUtil
 *
 * @author 900045
 * date By 2021-11-11 15:25:41
 */
public class NumberUtil {

	public NumberUtil() {
	}


	public static int compare(double x, double y) {
		BigDecimal val1 = BigDecimal.valueOf(x);
		BigDecimal val2 = BigDecimal.valueOf(y);
		return val1.compareTo(val2);
	}

	public static double ceil(double d, int len) {
		String str = Double.toString(d);
		int a = str.indexOf(Constant.POINT);
		if (a + 3 > str.length()) {
			a = str.length();
		} else {
			a += 3;
		}

		str = str.substring(0, a);
		return Double.parseDouble(str);
	}

	public static double ceil(double d) {
		return ceil(d, 2);
	}

	public static String format(double d) {
		if (d < 1.0E7D) {
			return d + "";
		} else {
			NumberFormat nf = NumberFormat.getInstance();
			nf.setGroupingUsed(false);
			return nf.format(d);
		}
	}

	public static long getLong(String str) {
		if (StringUtils.isBlank(str)) {
			return 0L;
		} else {
			long ret = 0L;

			try {
				ret = Long.parseLong(str);
			} catch (NumberFormatException var4) {
				ret = 0L;
			}

			return ret;
		}
	}

	public static int getInt(String str) {
		if (StringUtils.isBlank(str)) {
			return 0;
		} else {
			boolean var1 = false;

			int ret;
			try {
				ret = Integer.parseInt(str);
			} catch (NumberFormatException var3) {
				ret = 0;
			}

			return ret;
		}
	}

	public static double getDouble(String str) {
		if (StringUtils.isBlank(str)) {
			return 0.0D;
		} else {
			double ret = 0.0D;

			try {
				ret = Double.parseDouble(str);
			} catch (NumberFormatException var4) {
				ret = 0.0D;
			}

			return ret;
		}
	}

	public static String randomNum(int len) {
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < len; i++) {
			sb.append(random.nextInt(10));
		}
		return sb.toString();
	}


	public static String getStringRandom(int length) {
		String val = "";
		Random random = new Random();

		//参数length，表示生成几位随机数
		for (int i = 0; i < length; i++) {

			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
			//输出字母还是数字
			if ("char".equalsIgnoreCase(charOrNum)) {
				//输出是大写字母还是小写字母
				int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
				val += (char) (random.nextInt(26) + temp);
			} else if ("num".equalsIgnoreCase(charOrNum)) {
				val += String.valueOf(random.nextInt(10));
			}
		}
		return val;
	}

	public static String getOnlyStringRandom(int length) {
		String val = "";
		Random random = new Random();
		//参数length，表示生成几位随机数
		for (int i = 0; i < length; i++) {
			//输出是大写字母还是小写字母
			int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
			val += (char) (random.nextInt(26) + temp);
		}
		return val.toUpperCase();
	}
}
